package net.awpspace.mobile.management.house;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Vector;

import javax.microedition.io.Connector;

public class Persistence {

	private Vector list = null;
	private String file_path = "/housedata/housedata.dt";
	private static Persistence instance = null;

	private Persistence() {
		try {
			list = new Vector();
			readFile(file_path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("OK xong");
	}

	public static Persistence getInstance() {
		if (instance == null)
			instance = new Persistence();
		return instance;
	}

	public Vector getData() {
		return list;
	}

	public boolean add(HouseMember a, boolean b) {
		System.out.println("add");
		list.addElement(a);
		if(b)
		writeData();
		return true;
	}

	public boolean delete(int i) {
		if (i < 0 || i >= list.size())
			return false;
		else {
			list.removeElementAt(i);
		}
		return true;
	}

	public boolean delete(HouseMember a) {		
		return list.removeElement(a);		
	}

	public Vector search(String key){
		Vector data = new Vector();
		for(int i = 0; i < list.size(); i++){
			HouseMember mem = ((HouseMember)(list.elementAt(i)));
			if(mem.getName().toUpperCase().indexOf(key.toUpperCase()) != -1)
				data.addElement(mem);
		}
		return data;
	}
	public boolean updateHouseMember(HouseMember member){
		int index = -1;
		for(int i = 0; i < list.size(); i++){
			if(member.getId().equals(((HouseMember)list.elementAt(i)).getId())){
				index = i;
				list.removeElementAt(index);
				list.insertElementAt(member, index);
				return true;
			}
		}
		return false;
	}
	
	private void readFile(String filename) throws IOException {
		System.out.println("Read file");
		try {
			InputStreamReader reader = new InputStreamReader(

			getClass().getResourceAsStream(filename), "UTF-8");

			String line = null;
			while ((line = readLine(reader)) != null) {
				add(parserHouseMember(line), false);
			}
			reader.close();
		} catch (IOException e) {
			System.out.println("Loi: " + e.toString());
		} finally {

		}

	}

	private HouseMember parserHouseMember(String line) {
		// TODO Auto-generated method stub
		// id|name|sex|date|icard|address
		String id, name, date, icard, address, sex;
		String part = "|";
		int index;
		index = line.indexOf(part);
		id = line.substring(0, index);
		line = line.substring(index + 1);

		index = line.indexOf(part);
		name = line.substring(0, index);
		line = line.substring(index + 1);

		index = line.indexOf(part);
		sex = line.substring(0, index);
		line = line.substring(index + 1);

		index = line.indexOf(part);
		date = line.substring(0, index);
		line = line.substring(index + 1);

		index = line.indexOf(part);
		icard = line.substring(0, index);
		line = line.substring(index + 1);

		address = line;

		return new HouseMember(id, name, sex, new AwpDate(date), icard, address);
	}


	private String readLine(InputStreamReader reader) throws IOException {
		int readChar = reader.read();
		if (readChar == -1) {
			return null;
		}
		StringBuffer string = new StringBuffer("");

		while (readChar != -1 && readChar != '\n') {

			if (readChar != '\r') {

				string.append((char) readChar);

			}
			readChar = reader.read();
		}

		return string.toString();
	}
	
	public void writeData(){
		try {
			OutputStream os = Connector.openDataOutputStream("/res" + file_path);
			
//			 = fconn.openOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
			for(int i = 0; i < list.size(); i++){
				HouseMember m = (HouseMember)list.elementAt(i);
				osw.write(m.getId() + "|" + m.getName() + "|" + m.getSex() + "|" + m.getDate().toString() + "|" + m.getIcard() + "|" + m.getAddress() + "\n");
			}
			osw.close();
		} catch (Exception e) {
			System.out.println("Loi: " + e.toString());
		} finally {
			
		}
	}
}
